package com.mangohouse.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsStatics;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 
 * @TODO action基类
 * @author roye
 * @date Apr 17, 2013 4:18:19 PM
 * @version V1.0
 */
public class BaseAction extends ActionSupport{
	
	private static final long serialVersionUID = -1329659554188884314L;
	
	protected HttpServletRequest request = null;
	
	protected HttpServletResponse response  = null;
	
	protected HttpSession session = null;
	
	/*** 分页参数****/
	public Integer currentPage = 1;//当前页
	public Integer page_size = 15;//每页显示条数
	public Integer start_row = 0; 
	/**
	 * 
	 * @todo  初始化分页参数
	 * @author roye
	 * @date Apr 19, 2013 2:35:50 PM
	 * @param   
	 * @return int   
	 * @throws
	 */
	public BaseAction(){
		
		  /*String page = request.getParameter("page");  
          String rows = request.getParameter("rows");  
          currentPage = Integer.parseInt((page == null || page == "0") ? "1" : page);  
          page_size = Integer.parseInt((rows == null || rows == "0") ? "15" : rows);  
          // 每页的开始记录 第一页为1 第二页为number +1  
          start_row = (currentPage - 1) * page_size;  */
          
	}
	
    
    /**
     * 
     * @todo 往页面输出对象
     * @author roye
     * @date May 3, 2013 10:58:20 AM
     * @param   
     * @return void   
     * @throws
     */
    protected void write(Object object)throws IOException {
    	response.reset();
    	response.setContentType("text/html;charset=UTF-8");
	    PrintWriter printWriter = response.getWriter();
	    printWriter.println(object);
	    printWriter.flush();
	    printWriter.close();
	}
    
    /**
     * 
     * @todo 往页面输出JSON对象用于分页
     * @author roye
     * @date May 10, 2013 7:48:18 PM
     * @param   
     * @return void   
     * @throws
     */
    protected void writeJson(JSONArray array,int count)throws IOException{
    	response.reset();
    	response.setContentType("text/html;charset=UTF-8");
	    PrintWriter printWriter = response.getWriter();
	    Map<String, Object> json = new HashMap<String, Object>();  
        json.put("total", count);// total键 存放总记录数，必须的  
        json.put("rows", array);// rows键 存放每页记录 list  
        JSONObject jsonobj = new JSONObject();
        jsonobj = JSONObject.fromObject(json);// 格式化result一定要是JSONObject  
        printWriter.print(jsonobj);  
        printWriter.flush();  
        printWriter.close();  
    }
     
	protected HttpSession getSession() {
		return ServletActionContext.getRequest().getSession();
	}
	
	protected HttpServletRequest getRequest(){
		return (HttpServletRequest)ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);
	}
	
	protected HttpServletResponse getResponse(){
		return (HttpServletResponse)ActionContext.getContext().get(StrutsStatics.HTTP_RESPONSE);
	}
	
	
	
	/**
	 * 设置Request的Attribute函数
	 * 
	 * @param key
	 * @param value
	 */
	public void setRequestAttribute(String key, Object value) {
		this.getRequest().setAttribute(key, value);
	}

	/**
	 * 取得Request的Attribute存储值
	 * 
	 * @param key
	 * @return
	 */
	public Object getRequestAttribute(String key) {
		return this.getRequest().getAttribute(key);
	}

	/**
	 * 取得Request的Parameter存储值
	 * 
	 * @param key
	 * @return
	 */
	public String getRequestParameter(String key) {
		return this.getRequest().getParameter(key);
	}

	/**
	 * 设置Session的Attribute函数
	 * 
	 * @param key
	 * @param value
	 */
	public void setSessionAttribute(String key, Object value) {
		this.getSession().setAttribute(key, value);
	}

	/**
	 * 取得Session的Attribute存储值
	 * 
	 * @param key
	 * @return
	 */
	public Object getSessionAttribute(String key) {
	   return this.getSession().getAttribute(key);
	}

	/**
	 * 【取得ServletContext对象】
	 * 
	 * @return
	 */
	public ServletContext getServletContext() {
	   return ServletActionContext.getServletContext();
	}
}
